package net.netca.gdltax.bpms.businessmanage.utilits;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.Calendar;





public class StrHelper {

	public static String DeceToStr(int num,int len)		//ǰ�油0
	{
		String ret= "";
		String tmp= Integer.toString(num);
		int zeroLen= len-tmp.length();
		for(int i=0;i<zeroLen;i++)
			ret+='0';
		ret+= tmp;
		return ret;
	}
	public static String GetTimeStr(Date date,String sep)	//hh:mm:ss
	{
		if(sep==null)sep="";
		Calendar calendar= Calendar.getInstance();
		if(date!=null)
			calendar.setTime(date);
		String tstr= DeceToStr(calendar.get(Calendar.HOUR_OF_DAY),2);
		tstr+=sep+DeceToStr(calendar.get(Calendar.MINUTE),2);
		tstr+=sep+DeceToStr(calendar.get(Calendar.SECOND),2);
		return tstr;
	}
	public static String GetDateStr(Date date,String sep)	//YYYYMMDD
	{
		if(sep==null)sep="";
		Calendar calendar= Calendar.getInstance();
		if(date!=null)
			calendar.setTime(date);
		String str= Integer.toString(calendar.get(Calendar.YEAR));
		str+= sep+DeceToStr(calendar.get(Calendar.MONTH)+1,2);
		str+= sep+DeceToStr(calendar.get(Calendar.DATE),2);
		return str;
	}
	public static String GetMonthStr(Date date,String sep)	//YYYYMM
	{
		if(sep==null)sep="";
		Calendar calendar= Calendar.getInstance();
		if(date!=null)
			calendar.setTime(date);
		String str= Integer.toString(calendar.get(Calendar.YEAR));
		str+= sep+DeceToStr(calendar.get(Calendar.MONTH)+1,2);
		return str;
	}
	public static String GetDateTimeStr(Date date)	//YYYY-MM-DD hh:mm:ss
	{
		Calendar calendar= Calendar.getInstance();
		if(date!=null)
			calendar.setTime(date);
		String str= Integer.toString(calendar.get(Calendar.YEAR));
		str+= "-"+DeceToStr(calendar.get(Calendar.MONTH),2);
		str+= "-"+DeceToStr(calendar.get(Calendar.DATE),2);
		str+= " "+DeceToStr(calendar.get(Calendar.HOUR_OF_DAY),2);
		str+=":"+DeceToStr(calendar.get(Calendar.MINUTE),2);
		str+=":"+DeceToStr(calendar.get(Calendar.SECOND),2);
		return str;
	}
	public static Date GetDate(String str)	//YYYYMMDD��YYYY-MM-DD
	{
		try{
			int year,month,date;
			String strs[]=str.split("-");
			if(strs.length==1){
				if(str.length()!=8)return null;
				year= Integer.parseInt(str.substring(0,4));
				month= Integer.parseInt(str.substring(4,6))-1;	//0~11
				date= Integer.parseInt(str.substring(6));
			}
			else if(strs.length==3){
				year= Integer.parseInt(strs[0]);
				month= Integer.parseInt(strs[1])-1;
				date= Integer.parseInt(strs[2]);
			}
			else
				return null;

			Calendar calendar= Calendar.getInstance();
			calendar.set(year,month,date,0,0,0);
			return calendar.getTime();
		}catch(Exception e){
			return null;
		}
	}
	public static Date GetDateTime(String str)	//YYYY-MM-DD hh:mm:ss
	{
		try{
			String[] strs1= str.split(" ");
			if(strs1.length!=2)return null;
			String[] dstrs= strs1[0].split("-");
			if(dstrs.length!=3)return null;
			String[] tmstrs= strs1[1].split(":");
			if(tmstrs.length!=3)return null;
	
			int year= Integer.parseInt(dstrs[0]);
			int month= Integer.parseInt(dstrs[1])-1;
			int date= Integer.parseInt(dstrs[2]);
			int hour= Integer.parseInt(tmstrs[0]);
			int minute= Integer.parseInt(tmstrs[1]);
			int second= Integer.parseInt(tmstrs[2]);
	
			Calendar calendar= Calendar.getInstance();
			calendar.set(year,month,date,hour,minute,second);
			return calendar.getTime();
		}catch(Exception e){
			return null;
		}
	}
	public static boolean isAllNumber(String str)	//�ִ��Ƿ�ȫ������
	{
		for(int i=0;i<str.length();i++)
			if(str.charAt(i)>'9' || str.charAt(i)<'0')
				return false;
		return true;
	}
	public static boolean isAllLetter(String str)	//�ִ��Ƿ�ȫ����ĸ	'A'-'Z' || 'a'-'z'
	{
		for(int i=0;i<str.length();i++)
		{
			char c= str.charAt(i);
			if(c<'A' || c>'Z'&&c<'a' || c>'z')
				return false;
		}
		return true;
	}
	public static boolean ChkIp(String ip)
	{
		String[] tmp= ip.split("\\.");
		if(tmp.length!=4)return false;
		for(int i=0;i<4;i++)
		{
			try{
			int t= Integer.parseInt(tmp[i]);
			if(t>255 || t<0)return false;
			}catch(Exception e){return false;}
		}
		return true;
	}
	/*
	 * "&"->"&amp;"
	 * "<"->"&lt;"
	 * ">"->"&gt;"
	 * "'"->"&apos;"
	 * """->"&quot;"
	 * "\r\n"->"<br/>\r\n"
	 */
	public static String ConvertToXml(String string)
	{
		if(string==null)return null;
		String str=string.replaceAll("&","&amp;");
		str=str.replaceAll(">","&gt;");
		str=str.replaceAll("<","&lt;");
		str=str.replaceAll("'","&apos;");
		str=str.replaceAll("\"","&quot;");
		str=str.replaceAll("\r\n","\n");
		str=str.replaceAll("\n","<br/>\r\n");
		return str;
	}
	/*
	 * "&"->"&amp;"
	 * "<"->"&lt;"
	 * ">"->"&gt;"
	 * """->"&quot;"
	 */
	public static String ConvertToHtml(String string)
	{
		if(string==null)return null;
		String str=string.replaceAll("&","&amp;");
		str=str.replaceAll(">","&gt;");
		str=str.replaceAll("<","&lt;");
		str=str.replaceAll("\"","&quot;");
		return str;
	}
	/*
	 * "\"->"\\"
	 * "'"->"\'"
	 * """->"\""
	 * 
	 * "\r"->"\\r"
	 * "\n"->"\\n"
	 * "_"->"\_"	ֻ������LIKE���
	 * "%"->"\%"	ֻ������LIKE���
	 */
	public static String ConvertToSQL(String str)
	{
		if(str==null)return null;
		String string=str.replaceAll("\\\\","\\\\\\\\");
		string=string.replaceAll("'","\\\\'");
		string=string.replaceAll("\"","\\\\\"");
//		string=string.replaceAll("%","\\\\%");
//		string=string.replaceAll("_","\\\\_");
		return string;
	}
	
	//��ȡ�ļ����(��׺
	public static String  GetfnWithoutSuf(String filename)
	{	
		if(filename==null)
			return null;
		String name=null;
		int index=filename.lastIndexOf(".");
		if(index==-1)
			return null;
		name=filename.substring(0,index);
		return name;
	}
	
	//���ļ���һ��String
	public static String loadAFileToString(File f) throws IOException { 
	        InputStream is = null;
	        String ret = null;
	        try {
	            is =  new FileInputStream(f) ;
	            long contentLength = f.length();
	            byte[] ba = new byte[(int)contentLength];
	            is.read(ba);
	            ret = new String(ba);
	        } catch(Exception e){
	        	
	        	e.printStackTrace();
	        }finally {
	            if(is!=null) {try{is.close();} catch(Exception e){} }
	        }
	        return ret;       
	    }
	public static String convertDateStr(String date) //YYYYMMDD->YYYY-MM-DD
	{
		String str1=null;
		String str2=null;
		String str3=null;
		str1=date.substring(0,4);
		str2=date.substring(4,6);
		str3=date.substring(6, 8);
		return str1+"-"+str2+"-"+str3;	
	}

	public static Date DateAddDate(Date tm,int add)
	{
		if(tm==null)return null;
		Calendar calendar= Calendar.getInstance();
		calendar.setTime(tm);
		calendar.add(Calendar.DATE,add);
		return calendar.getTime();
	}


	
	
	
	
	
}
